package com.example.peibei.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.dingtao.common.bean.Dynamic;
import com.dingtao.common.bean.RefreshResult;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDActivity;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.GlideUtils;
import com.dingtao.common.util.SPUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.FollowSquareListPresenter;
import com.example.peibei.ui.fragment.SquareDetailListFragment;
import com.example.peibei.ui.fragment.SquareListFragment;
import com.flyco.tablayout.SlidingTabLayout;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.gyf.immersionbar.BarHide;
import com.gyf.immersionbar.ImmersionBar;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class SquareDetailActivity extends WDActivity {
    @BindView(R.id.slide_tablayout)
    SlidingTabLayout tabLayout;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.iv_head)
    ImageView iv_head;
    @BindView(R.id.iv_bg)
    ImageView iv_bg;
    @BindView(R.id.tv_nickname)
    TextView tv_nickname;
    @BindView(R.id.tv_id)
    TextView tv_id;
    @BindView(R.id.ll_no_follow)
    LinearLayout ll_no_follow;
    @BindView(R.id.ll_followed)
    LinearLayout ll_followed;

    private String[] title = {"热门", "最新", "图文", "纯文字"};
    private ArrayList<Fragment> fragments = new ArrayList<>();
    private SPUtils spUtils;
    private String token;
    private String id;
    private FollowSquareListPresenter giveUpPresenter;
    private boolean follow;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_square_detail;
    }

    @Override
    protected void initView() {
        ImmersionBar.with(this)
                .fitsSystemWindows(false)
                .statusBarDarkFont(false)
                .hideBar(BarHide.FLAG_HIDE_STATUS_BAR)
                .init();
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        String imgUrl = bundle.getString("imgUrl");
        String backUrl = bundle.getString("backUrl");
        String circleName = bundle.getString("circleName");
        String circleTypeId = bundle.getString("circleTypeId");
        id = bundle.getString("id");
        String personNum = bundle.getString("personNum");
        follow = bundle.getBoolean("follow");
        tv_nickname.setText(circleName);
        tv_id.setText(personNum + "人已加入");
        GlideUtils.loadImg(this, imgUrl, 20, iv_head);
        if (!TextUtils.isEmpty(backUrl)) {
            Glide.with(this).load(backUrl).into(iv_bg);
        }
        if (follow) {
            ll_followed.setVisibility(View.VISIBLE);
            ll_no_follow.setVisibility(View.GONE);
        } else {
            ll_followed.setVisibility(View.GONE);
            ll_no_follow.setVisibility(View.VISIBLE);
        }
        spUtils = new SPUtils(this, SpConstant.USER_TOKEN);
        token = spUtils.getString(SpConstant.USER_TOKEN);
        giveUpPresenter = new FollowSquareListPresenter(new FollowCall());
        fragments.add(new SquareDetailListFragment(id, "1"));
        fragments.add(new SquareDetailListFragment(id, "2"));
        fragments.add(new SquareDetailListFragment(id, "3"));
        fragments.add(new SquareDetailListFragment(id, "4"));
        viewpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return title[position];
            }
        });
        tabLayout.setViewPager(viewpager, title, this, fragments);
        TextView title1 = tabLayout.getTitleView(0);
        TextView title2 = tabLayout.getTitleView(1);
        TextView title3 = tabLayout.getTitleView(2);
        TextView title4 = tabLayout.getTitleView(3);
        title1.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        title2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
        title3.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
        title4.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                int tabCount = tabLayout.getTabCount();
                for (int i = 0; i < tabCount; i++) {
                    TextView title = tabLayout.getTitleView(i);
                    if (i == position) {
                        title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
                    } else {
                        title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @OnClick(R.id.iv_back)
    public void back() {
        finish();
    }

    @OnClick(R.id.fl_follow)
    public void follow() {
        if (!TextUtils.isEmpty(token)) {
            giveUpPresenter.reqeust(follow, "Bearer " + token, id);
        } else {
            intent(LoginActivity.class);
        }
    }

    @OnClick(R.id.fb_add)
    public void addSquare() {
        Bundle bundle = new Bundle();
        bundle.putString("id", id);
        intent(SendSquareActivity.class, bundle);
    }

    class FollowCall implements DataCall<Dynamic> {

        @Override
        public void success(Dynamic data, Object... args) {
            follow = !follow;
            if (follow) {
                ll_followed.setVisibility(View.VISIBLE);
                ll_no_follow.setVisibility(View.GONE);
            } else {
                ll_followed.setVisibility(View.GONE);
                ll_no_follow.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void fail(ApiException e, Object... args) {
            EventBus.getDefault().post(RefreshResult.getInstance(true, true));
        }
    }

    @Override
    protected void destoryData() {

    }
}
